#include "stdafx.h"
#include "wf_polylinedelbetween.h"
#include "pf_polyline.h"

#include "pf_snapper.h"



WF_PolylineDelBetween::WF_PolylineDelBetween(PF_Container& container,
		PF_GraphicView& graphicView)
		:PF_PreviewActionInterface("Delete between two nodes",
						   container, graphicView) {}


QAction* WF_PolylineDelBetween::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	QAction* action = new QAction(tr("Delete &between two nodes"), NULL);
	action->setShortcut(QKeySequence());
	action->setStatusTip(tr("Delete between two nodes"));
	return action;
}

void WF_PolylineDelBetween::init(int status) {
	PF_ActionInterface::init(status);
	delEntity = delSegment = NULL;
	nodePoint1 = nodePoint2 = PF_Vector(false);
}



void WF_PolylineDelBetween::trigger() {

	PF_DEBUG->print("PF_ActionPolylineDelBetween::trigger()");

	if (delEntity!=NULL && delSegment->isAtomic() && nodePoint1.valid && nodePoint2.valid) {

		delEntity->setHighlighted(false);
		graphicView->drawEntity(delEntity);

		PF_Modification m(*container, graphicView);
		delEntity = m.deletePolylineNodesBetween((PF_Polyline&)*delEntity, (PF_AtomicEntity&)*delSegment, nodePoint1, nodePoint2 );

                delEntity = delSegment = NULL;
		nodePoint1 = nodePoint2 = PF_Vector(false);
		setStatus(SetNodePoint1);

		PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
	}
////////////////////////////////////////2006/06/15
		graphicView->redraw();
////////////////////////////////////////
}



void WF_PolylineDelBetween::mouseMoveEvent(PF_MouseEvent* e) {
	PF_DEBUG->print("PF_ActionPolylineDelBetween::mouseMoveEvent begin");

	switch (getStatus()) {
	case ChooseSegment:
		break;

	case SetNodePoint1:
		snapPoint(e);
		break;

	case SetNodePoint2:
		snapPoint(e);
		break;
	default:
		break;
	}

	PF_DEBUG->print("PF_ActionPolylineDelBetween::mouseMoveEvent end");
}



void WF_PolylineDelBetween::mouseReleaseEvent(PF_MouseEvent* e) {
        if (e->button()==Qt::LeftButton) {
		switch (getStatus()) {
		case ChooseSegment:
			delEntity = catchEntity(e);
			if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (delEntity->rtti()!=LX_Define::EntityPolyline) {

				PF_DIALOGFACTORY->commandMessage(
					tr("Entity must be a polyline."));
			} else {
				PF_Vector clickCoord = snapPoint(e);
				delSegment = NULL;
				double dist = graphicView->toGraphDX(snapRange)*0.9;
				delSegment =  (PF_AtomicEntity*)((PF_Polyline*)delEntity)->getNearestEntity( PF_Vector(graphicView->toGraphX(e->x()),
									graphicView->toGraphY(e->y())), &dist, LX_Define::ResolveNone);
				if(delSegment == NULL)
					break;
				delEntity->setHighlighted(true);
				graphicView->drawEntity(delEntity);
				setStatus(SetNodePoint1);
////////////////////////////////////////2006/06/15
		graphicView->redraw();
////////////////////////////////////////
			}
			break;

		case SetNodePoint1:
			nodePoint1 = snapPoint(e);
			if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (!nodePoint1.valid) {
				PF_DIALOGFACTORY->commandMessage(tr("Deletinging point is invalid."));
			} else if (!delEntity->isPointOnEntity(nodePoint1)) {
				PF_DIALOGFACTORY->commandMessage(
					tr("Deleting point is not on entity."));
			}else{
				setStatus(SetNodePoint2);
			}
			break;
		case SetNodePoint2:
			nodePoint2 = snapPoint(e);
			if (delEntity==NULL) {
				PF_DIALOGFACTORY->commandMessage(tr("No Entity found."));
			} else if (!nodePoint2.valid) {
				PF_DIALOGFACTORY->commandMessage(tr("Deletinging point is invalid."));
			} else if (!delEntity->isPointOnEntity(nodePoint2)) {
				PF_DIALOGFACTORY->commandMessage(
					tr("Deleteinging point is not on entity."));
			} else {
				deleteSnapper();
				trigger();
			}
			break;

		default:
			break;
		}
        } else if (e->button()==Qt::RightButton) {
		deleteSnapper();
		if (delEntity!=NULL) {
			delEntity->setHighlighted(false);
			graphicView->drawEntity(delEntity);
////////////////////////////////////////2006/06/15
		graphicView->redraw();
////////////////////////////////////////
		}
		init(getStatus()-1);
	}
}


void WF_PolylineDelBetween::updateMouseButtonHints() {
	switch (getStatus()) {
	case ChooseSegment:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify polyline to delete between two nodes"),
											tr("Cancel"));
		break;
	case SetNodePoint1:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first node"),
											tr("Back"));
		break;
	case SetNodePoint2:
		PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second node"),
											tr("Back"));
		break;
	default:
		PF_DIALOGFACTORY->updateMouseWidget("", "");
		break;
	}
}



void WF_PolylineDelBetween::updateMouseCursor() {
	graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_PolylineDelBetween::updateToolBar() {
	switch (getStatus()) {
	case SetNodePoint1:
	case SetNodePoint2:
	case ChooseSegment:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
		break;
	default:
		PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarPolylines);
		break;
	}
}


// EOF
